<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleYearDialog">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
       <el-table-column label="学年" align="center" prop="applyYear"/>
      <!-- <el-table-column label="1：提交，2：班级" align="center" prop="" /> -->
      <el-table-column label="申请人姓名" align="center" prop="xm" />
      <el-table-column label="性别" align="center" prop="xb" />
      <el-table-column label="民族" align="center" prop="mz" />
      <el-table-column label="身份证号码" align="center" prop="sfzhm" />
      <el-table-column label="学号" align="center" prop="xh" />
      <el-table-column label="出生年月" align="center" prop="csny" />
      <el-table-column label="是否提交" align="center" prop="step">
        <template slot-scope="scope">
          <!-- <div v-if="scope.row.step"> -->
            <div style="color: #FF0000;" v-if="scope.row.step == 0">
              未提交
            </div>
          <!-- </div> -->
          <div v-else style="color: #55aa00;">已提交</div>
        </template>
      </el-table-column>

      <el-table-column label="辅导员审核意见" align="center" prop="xsqmyj">
        <template slot-scope="scope">
          <div v-if="scope.row.bjpyyj">
            <div v-if="scope.row.bjpyyj == '1'" style="color: #55aa00;">
              特别困难
            </div>
            <div v-if="scope.row.bjpyyj == '2'" style="color: #55aa00;">
              比较困难
            </div>
            <div v-if="scope.row.bjpyyj == '3'" style="color: #55aa00;">
              一般困难
            </div>
            <div v-if="scope.row.bjpyyj == '4'" style="color: #ff5500;">
              不困难
            </div>
          </div>
          <div v-else style="color: #FF0000;">未审核</div>
        </template>
      </el-table-column>
      <el-table-column label="学院审核意见" align="center" prop="xsqmyj">
        <template slot-scope="scope">
          <div v-if="scope.row.ejxyldqmyj">
            <div v-if="scope.row.ejxyldqmyj == '1'" style="color: #55aa00;">
              特别困难
            </div>
            <div v-if="scope.row.ejxyldqmyj == '2'" style="color: #55aa00;">
              比较困难
            </div>
            <div v-if="scope.row.ejxyldqmyj == '3'" style="color: #55aa00;">
              一般困难
            </div>
            <div v-if="scope.row.ejxyldqmyj == '4'" style="color: #ff5500;" >
              不困难
            </div>
          </div>
          <div v-else style="color: #FF0000;">未审核</div>
        </template>
      </el-table-column>
      <el-table-column label="学校审核意见" align="center" prop="xsqmyj">
        <template slot-scope="scope">
          <div v-if="scope.row.xsqmyj">
            <div v-if="scope.row.xsqmyj == '1'"  style="color: #55aa00;">
              特别困难
            </div>
            <div v-if="scope.row.xsqmyj == '2'"  style="color: #55aa00;">
              比较困难
            </div>
            <div v-if="scope.row.xsqmyj == '3'"  style="color: #55aa00;">
              一般困难
            </div>
            <div v-if="scope.row.xsqmyj == '4'"  style="color: #ff5500;">
              不困难
            </div>
          </div>
          <div v-else style="color: #FF0000;">未审核</div>
        </template>
      </el-table-column>
      <el-table-column label="最终审核结果" align="center" prop="step">
       <template slot-scope="scope">
         <div v-if="scope.row.xsqmyj">
           <div v-if="scope.row.xsqmyj == '1'" style="color: #55aa00;">
             特别困难
           </div>
           <div v-if="scope.row.xsqmyj == '2'" style="color: #55aa00;">
             比较困难
           </div>
           <div v-if="scope.row.xsqmyj == '3'" style="color: #55aa00;">
             一般困难
           </div>
           <div v-if="scope.row.xsqmyj == '4'" style="color: #ff5500;">
             不困难
           </div>
         </div>
         <div v-else style="color: #FF0000;">审核中</div>
         </template>
      </el-table-column>
      <!-- <el-table-column label="当前审核流程" align="center" prop="step">
         <template slot-scope="scope">
           <div v-if="scope.row.step == 2">
             辅导员审核中
           </div>
           <div v-if="scope.row.step == 3">
             二级学院审核中
           </div>
           <div v-if="scope.row.step == 4">
             学校审核中
           </div>
           <div v-if="scope.row.step == 5"  style="color: #55aa00;">
             审核通过
           </div>
           <div v-if="scope.row.step == 6"  style="color: #FF0000;">
             已拒绝
           </div>
           <div v-if="scope.row.step == 7"  style="color: #ff5500;">
             已驳回
           </div>
         </template>
       </el-table-column> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-if="(scope.row.bjpyyj ==null || scope.row.bjpyyj =='' ) && scope.row.step == 0 ">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleRevoke(scope.row)"
              v-if="(scope.row.bjpyyj ==null || scope.row.bjpyyj =='') && scope.row.step == 2 ">撤回</el-button>

              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-if="scope.row.step == 0 ">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="showDetail(scope.row)"
            v-if="scope.row.step >=2">详情</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"></pagination>


    <!-- 添加或修改困难认定对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="1：提交，2：班级" prop="step">
          <el-input v-model="form.step" placeholder="请输入1：提交，2：班级" />
        </el-form-item>
        <el-form-item label="申请人姓名" prop="xm">
          <el-input v-model="form.xm" placeholder="请输入申请人姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="xb">
          <el-input v-model="form.xb" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="民族" prop="mz">
          <el-input v-model="form.mz" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="身份证号码" prop="sfzhm">
          <el-input v-model="form.sfzhm" placeholder="请输入身份证号码" />
        </el-form-item>
        <el-form-item label="学号" prop="xh">
          <el-input v-model="form.xh" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="出生年月" prop="csny">
          <el-input v-model="form.csny" placeholder="请输入出生年月" />
        </el-form-item>
        <el-form-item label="困难类型1" prop="knlx">
          <el-input v-model="form.knlx" placeholder="请输入困难类型1" />
        </el-form-item>
        <el-form-item label="困难类型2" prop="knlx2">
          <el-input v-model="form.knlx2" placeholder="请输入困难类型2" />
        </el-form-item>
        <el-form-item label="人口总数" prop="rkzs">
          <el-input v-model="form.rkzs" placeholder="请输入人口总数" />
        </el-form-item>
        <el-form-item label="收入来源" prop="srly">
          <el-input v-model="form.srly" placeholder="请输入收入来源" />
        </el-form-item>
        <el-form-item label="1城镇，2农村" prop="hjxz">
          <el-input v-model="form.hjxz" placeholder="请输入1城镇，2农村" />
        </el-form-item>
        <el-form-item label="户籍所在地" prop="hjszd">
          <el-input v-model="form.hjszd" placeholder="请输入户籍所在地" />
        </el-form-item>
        <el-form-item label="家庭成员" prop="jtcy">
          <el-input v-model="form.jtcy" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="家庭居住房产" prop="jtjzfqk">
          <el-input v-model="form.jtjzfqk" placeholder="请输入家庭居住房产" />
        </el-form-item>
        <el-form-item label="家庭汽车情况，1自用，2经营用，3无汽车" prop="jtqcqk">
          <el-input v-model="form.jtqcqk" placeholder="请输入家庭汽车情况，1自用，2经营用，3无汽车" />
        </el-form-item>
        <el-form-item label="家庭居住现居住地址" prop="jtxjzdz">
          <el-input v-model="form.jtxjzdz" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="家长电话" prop="jzdh">
          <el-input v-model="form.jzdh" placeholder="请输入家长电话" />
        </el-form-item>
        <el-form-item label="家庭年收入" prop="jtnsr">
          <el-input v-model="form.jtnsr" placeholder="请输入家庭年收入" />
        </el-form-item>
        <el-form-item label="人均年收入，人均年收入=家庭年收入÷家庭人口数" prop="rjnsr">
          <el-input v-model="form.rjnsr" placeholder="请输入人均年收入，人均年收入=家庭年收入÷家庭人口数" />
        </el-form-item>
        <el-form-item label="家庭经济主要收入来源情况,1机关事业单位公职人员,2单位公司合同制职工,3私营业主,4个体工商户,5务工,6务农,7其他收入,8无固定收入,9无收入" prop="jtzysr">
          <el-input v-model="form.jtzysr"
            placeholder="请输入家庭经济主要收入来源情况,1机关事业单位公职人员,2单位公司合同制职工,3私营业主,4个体工商户,5务工,6务农,7其他收入,8无固定收入,9无收入" />
        </el-form-item>
        <el-form-item label="学生父母丧失劳动能力或劳动能力弱" prop="ssldnl">
          <el-input v-model="form.ssldnl" placeholder="请输入学生父母丧失劳动能力或劳动能力弱" />
        </el-form-item>
        <el-form-item label="其中长期患病或残疾" prop="hbcjr">
          <el-input v-model="form.hbcjr" placeholder="请输入其中长期患病或残疾" />
        </el-form-item>
        <el-form-item label="需赡养丧失劳动能力的共同生活家庭成员" prop="zyssldnl">
          <el-input v-model="form.zyssldnl" placeholder="请输入需赡养丧失劳动能力的共同生活家庭成员" />
        </el-form-item>
        <el-form-item label="突发事件,1家庭遭受疫情,2家庭遭受自然灾害,3家庭遭受突发意外事件,4家庭欠债,5其它情况" prop="tfsj">
          <el-input v-model="form.tfsj" placeholder="请输入突发事件,1家庭遭受疫情,2家庭遭受自然灾害,3家庭遭受突发意外事件,4家庭欠债,5其它情况" />
        </el-form-item>
        <el-form-item label="申请人签名" prop="sqrqm">
          <el-input v-model="form.sqrqm" placeholder="请输入申请人签名" />
        </el-form-item>
        <el-form-item label="申请人签名日期" prop="sqrqmrq">
          <el-input v-model="form.sqrqmrq" placeholder="请输入申请人签名日期" />
        </el-form-item>
        <el-form-item label="班级评议意见，1特别困难" prop="bjpyyj">
          <el-input v-model="form.bjpyyj" placeholder="请输入班级评议意见，1特别困难" />
        </el-form-item>
        <el-form-item label="评议小组组长签名" prop="pyxzzzqm">
          <el-input v-model="form.pyxzzzqm" placeholder="请输入评议小组组长签名" />
        </el-form-item>
        <el-form-item label="评议小组组长签名日期" prop="pyxzzzqmrq">
          <el-input v-model="form.pyxzzzqmrq" placeholder="请输入评议小组组长签名日期" />
        </el-form-item>
        <el-form-item label="辅导员签名" prop="fdyqm">
          <el-input v-model="form.fdyqm" placeholder="请输入辅导员签名" />
        </el-form-item>
        <el-form-item label="辅导员签名日期" prop="fdyqmrq">
          <el-input v-model="form.fdyqmrq" placeholder="请输入辅导员签名日期" />
        </el-form-item>
        <el-form-item label="二级学院领导意见，1特别困难" prop="ejxyldqmyj">
          <el-input v-model="form.ejxyldqmyj" placeholder="请输入二级学院领导意见，1特别困难" />
        </el-form-item>
        <el-form-item label="二级学院领导签名" prop="ejxyldqm">
          <el-input v-model="form.ejxyldqm" placeholder="请输入二级学院领导签名" />
        </el-form-item>
        <el-form-item label="二级学院领导签名日期" prop="ejxyldqmrq">
          <el-input v-model="form.ejxyldqmrq" placeholder="请输入二级学院领导签名日期" />
        </el-form-item>
        <el-form-item label="学校审核意见，1特别困难" prop="xsqmyj">
          <el-input v-model="form.xsqmyj" placeholder="请输入学校审核意见，1特别困难" />
        </el-form-item>
        <el-form-item label="学校签名" prop="xsqm">
          <el-input v-model="form.xsqm" placeholder="请输入学校签名" />
        </el-form-item>
        <el-form-item label="学校签名日期" prop="xsqmrq">
          <el-input v-model="form.xsqmrq" placeholder="请输入学校签名日期" />
        </el-form-item>
        <el-form-item label="创建人ID" prop="createId">
          <el-input v-model="form.createId" placeholder="请输入创建人ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择学年弹窗 -->
    <el-dialog
      title="请选择学年"
      :visible.sync="stuYearDialogVisible"
      width="30%"
    >
        <el-select v-model="stuYearId" placeholder="请选择">
          <el-option
            v-for="item in enableYeas"
            :key="item.id"
            :label="item.stuYearName"
            :value="item.id">
          </el-option>
        </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="stuYearDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="yearConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listAllEnableYear,listAllYear} from "@/api/stuCQS/basedata/year";
  import {
    listOwn as listApply,
    getApply,
    delApply,
    addApply,
    updateApply,
    vaild,
    revoke
  } from "@/api/zxj/poverty/apply";
  import {
    Message
  } from 'element-ui';
  export default {
    name: "Apply",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 困难认定表格数据
        applyList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          step: null,
          xm: null,
          xb: null,
          mz: null,
          sfzhm: null,
          xh: null,
          csny: null,
          knlx: null,
          knlx2: null,
          rkzs: null,
          srly: null,
          hjxz: null,
          hjszd: null,
          jtcy: null,
          jtjzfqk: null,
          jtqcqk: null,
          jtxjzdz: null,
          jzdh: null,
          jtnsr: null,
          rjnsr: null,
          jtzysr: null,
          ssldnl: null,
          hbcjr: null,
          zyssldnl: null,
          tfsj: null,
          sqrqm: null,
          sqrqmrq: null,
          bjpyyj: null,
          pyxzzzqm: null,
          pyxzzzqmrq: null,
          fdyqm: null,
          fdyqmrq: null,
          ejxyldqmyj: null,
          ejxyldqm: null,
          ejxyldqmrq: null,
          xsqmyj: null,
          xsqm: null,
          xsqmrq: null,
          createId: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          fdyqmrq: [{
            required: true,
            message: "辅导员签名日期不能为空",
            trigger: "blur"
          }],
        },
        //所有启用得年份
        enableYeas:[],
        //选择的年份
        stuYearId:null,
        //学年对话框开关
        stuYearDialogVisible:false,
      };
    },
    created() {
      this.getList();
      this.listAllStuYear();
    },
    methods: {
      
      //查所有年份
      listAllStuYear(){
        listAllYear().then(res=>{
            if(res.code == 200){
               this.enableYeas = res.data;
               this.stuYearId = this.enableYeas[0].id;
            }
           
        });
      },
      //确认选择学年
      yearConfirm(){
        this.handleAdd();
      },
      //选择学年对话框
      handleYearDialog(){
          //只激活一个学年就不选了
          if(this.enableYeas.length == 1){
            this.handleAdd();
          }
          else{
            this.stuYearDialogVisible = true;

          }
      },
      /** 查询困难认定列表 */
      getList() {
        this.loading = true;
        listApply(this.queryParams).then(response => {
          this.applyList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          step: null,
          xm: null,
          xb: null,
          mz: null,
          sfzhm: null,
          xh: null,
          csny: null,
          knlx: null,
          knlx2: null,
          rkzs: null,
          srly: null,
          hjxz: null,
          hjszd: null,
          jtcy: null,
          jtjzfqk: null,
          jtqcqk: null,
          jtxjzdz: null,
          jzdh: null,
          jtnsr: null,
          rjnsr: null,
          jtzysr: null,
          ssldnl: null,
          hbcjr: null,
          zyssldnl: null,
          tfsj: null,
          sqrqm: null,
          sqrqmrq: null,
          bjpyyj: null,
          pyxzzzqm: null,
          pyxzzzqmrq: null,
          fdyqm: null,
          fdyqmrq: null,
          ejxyldqmyj: null,
          ejxyldqm: null,
          ejxyldqmrq: null,
          xsqmyj: null,
          xsqm: null,
          xsqmrq: null,
          createId: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {

        vaild({"stuYearId":this.stuYearId}).then(res=>{
            if(res.valid == 0){
                Message({
                  message: '本学年已经存在申请记录',
                  type: 'error'
                });
                
            }else{
                //this.stuYearDialogVisible = false;
                //将学生选择的学年保存带会话中
                for(let i = 0;i< this.enableYeas.length;i++){
                  if(this.enableYeas[i].id == this.stuYearId){
                     let data = {"stuYearName":this.enableYeas[i].stuYearName,"id":this.stuYearId};
                     sessionStorage.setItem("knrdSelectedYear",JSON.stringify(data));
                     break;
                  }
                }
               
                this.$router.push({
                  path: "/hard/pks/poverty/edit"
                })
            }
          
        })
        // if (this.applyList.length > 0) {
        //   Message({
        //     message: '已存在申请记录',
        //     type: 'error'
        //   })
        //   return
        // }
        // this.open=true
      
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        this.$router.push({
          path: "/hard/pks/poverty/edit",
          query: {
            id: id,
            edit:1
          }
        })
        // getApply(id).then(response => {
        //   this.form = response.data;
        //   this.open = true;
        //   this.title = "修改困难认定";
        // });
      },
      showDetail(row) {
        this.reset();
        const id = row.id || this.ids
        this.$router.push({
          path: "/hard/pks/poverty/edit",
          query: {
            id: id,
            show:1
          }
        })
        // getApply(id).then(response => {
        //   this.form = response.data;
        //   this.open = true;
        //   this.title = "修改困难认定";
        // });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateApply(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addApply(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除困难认定编号为"' + ids + '"的数据项？').then(function() {
          return delApply(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },

      handleRevoke(row) {
        
        this.$modal.confirm('是否撤回该记录？').then(function() {
          return revoke(row);
        }).then((res) => {
           this.getList();
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/apply/export', {
          ...this.queryParams
        }, `apply_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
